import 'package:extended_image/extended_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:largefriends/entity/entities/lf_photo_entity.dart';
import 'package:largefriends/net/lf_api_util.dart';
import 'package:largefriends/pages/lf_profile/lf_profile_gallery/lf_profile_gallery_logic.dart';
import 'package:largefriends/res/lf_color.dart';
import 'package:largefriends/res/lf_double_ext.dart';
import 'package:largefriends/res/lf_fonts.dart';
import 'package:largefriends/res/lf_text_style.dart';
import 'package:largefriends/res/strings/lf_strings.dart';
import 'package:largefriends/utils/lf_scroll_behavior.dart';
import 'package:largefriends/widgets/custom_cache_image.dart';
import 'package:largefriends/widgets/lf_button.dart';
import 'package:largefriends/widgets/preload_page_view.dart';

class LfProfilePhotoGalleryPage extends StatefulWidget {
  const LfProfilePhotoGalleryPage({super.key});

  @override
  State<LfProfilePhotoGalleryPage> createState() =>
      _LfProfilePhotoGalleryPageState();
}

class _LfProfilePhotoGalleryPageState extends State<LfProfilePhotoGalleryPage> {
  final logic = Get.put(LfProfileGalleryLogic());
  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        child: Scaffold(
          appBar: AppBar(
            backgroundColor: Colors.black26,
            leading: InkWell(
              child: Icon(
                Icons.arrow_back,
                color: colorFEFEFE,
              ),
              onTap: () {
                Get.back();
              },
            ),
            automaticallyImplyLeading: false,
            centerTitle: false,
            title: GetBuilder<LfProfileGalleryLogic>(
              builder: (controller) {
                return Obx(() {
                  bool isPrivate =
                      controller.photos[controller.currentIndex.value].type ==
                          photoTypePrivate;
                  return Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      Text(
                        "${controller.currentIndex.value + 1}/${controller.total}",
                        style: lfTextStyle(
                          sp: 16.sp,
                          fontFamily: fontBold,
                          color: colorFEFEFE,
                        ),
                      ),
                      5.w.sbw,
                      Opacity(
                        opacity: isPrivate ? 1 : 0,
                        child: Icon(
                          Icons.lock_rounded,
                          size: 20.r,
                          color: colorFEFEFE,
                        ),
                      ),
                      20.w.sbw
                    ],
                  );
                });
              },
            ),
            // actions: [Image.asset(Res.)],
          ),
          extendBodyBehindAppBar: true,
          body: GetBuilder<LfProfileGalleryLogic>(
            builder: (controller) {
              return ScrollConfiguration(
                behavior: LfScrollBehavior(),
                child: PreloadPageView.builder(
                  itemBuilder: (context, index) {
                    if (controller.photos[index].url?.startsWith("http") ==
                        true) {
                      return InkWell(
                        onTap: () {
                          Get.back();
                        },
                        child: Container(
                          width: ScreenUtil().screenWidth,
                          height: ScreenUtil().screenHeight,
                          alignment: Alignment.center,
                          color: Colors.black,
                          child: CustomCacheImage(
                            imageUrl: controller.photos[index].url,
                            extendedImageMode: ExtendedImageMode.gesture,
                          ),
                        ),
                      );
                    } else {
                      bool requested =
                          controller.userEntity.privateAlbumStatus == 2;
                      return Container(
                        width: ScreenUtil().screenWidth,
                        height: ScreenUtil().screenHeight,
                        alignment: Alignment.center,
                        color: Colors.black,
                        child: Stack(
                          fit: StackFit.expand,
                          children: [
                            Center(
                              child: CustomCacheImage(
                                imageUrl: controller.photos[index].url,
                                extendedImageMode: ExtendedImageMode.gesture,
                              ),
                            ),
                            Align(
                              alignment: Alignment.bottomCenter,
                              child: Padding(
                                padding: EdgeInsets.symmetric(
                                  horizontal: 40.w,
                                  vertical: 20.h,
                                ),
                                child: SizedBox(
                                  height: 50.h,
                                  child: LfButton(
                                    enabled: !requested,
                                    text: requested
                                        ? strAccessRequested.tr
                                        : strRequestAccess.tr,
                                    onPress: () {
                                      requestPrivatePhoto(
                                          controller.userEntity);
                                    },
                                  ),
                                ),
                              ),
                            )
                          ],
                        ),
                      );
                    }
                  },
                  itemCount: controller.photos.length,
                  controller: controller.controller,
                  preloadPagesCount: controller.photos.length,
                  onPageChanged: (value) {
                    controller.currentIndex.value = value;
                  },
                ),
              );
            },
          ),
        ),
        onWillPop: () async {
          Get.back();
          return false;
        });
  }
}
